import {
  request,
  methods,
  request_address_cjx,
  request_address_dsl,
  // request_address,
  assemble1QueryParams,
} from '../utils/request';

const actions = {
  dataSearch: {
    name: 'dataSearch',
    url: '/dataSearch',
    source: 'cjx',
  },
  dataUpdate: {
    name: 'dataUpdate',
    url: '/dataUpdate',
    source: 'cjx',
  },

  getOptMap: {
    name: 'getOptMap',
    url: '/getOptMap',
    source: 'cjx',
  },
  dataAdd: {
    name: 'dataAdd',
    url: '/dataAdd',
    source: 'cjx',
  },
  dataFindDataCol: {
    name: 'dataFindDataCol',
    url: '/dataFindDataCol',
    source: 'cjx',
  },
  matchAuthor: {
    name: 'matchAuthor',
    url: '/matchAuthor',
    source: 'cjx',
  },
};

const customRequest = (
  action: any,
  token: string = '',
  formdata: any = null,
) => {
  let options: any = {
    headers: {
      'Content-Type': 'application/json;charset=utf-8',
      Accept: 'application/json',
      Authorization: token ? token : '',
    },
  };
  let url = action.url;
  switch (action.name) {
    case actions.dataSearch.name:
      options['method'] = methods.post;
      options['body'] = JSON.stringify(formdata);
      break;
    case actions.dataUpdate.name:
      options['method'] = methods.post;
      options['body'] = JSON.stringify(formdata);
      break;
    case actions.getOptMap.name:
      options['method'] = methods.get;
      break;
    case actions.dataAdd.name:
      options['method'] = methods.post;
      options['body'] = JSON.stringify(formdata);
      break;
    case actions.dataFindDataCol.name:
      options['method'] = methods.post;
      break;
    case actions.matchAuthor.name:
      options['method'] = methods.get;
      url = url + assemble1QueryParams(formdata);
      break;

    default:
  }

  if (action.source == 'cjx') {
    return request(request_address_cjx + url, options);
  }
  if (action.source == 'dsl') {
    return request(request_address_dsl + url, options);
  }
};

export function dataSearch(token: string = '', formdata: any = null) {
  return customRequest(actions.dataSearch, token, formdata);
}
export function dataUpdate(token: string = '', formdata: any = null) {
  return customRequest(actions.dataUpdate, token, formdata);
}
export function getOptMap(token: string = '', formdata: any = null) {
  return customRequest(actions.getOptMap, token, formdata);
}
export function dataAdd(token: string = '', formdata: any = null) {
  return customRequest(actions.dataAdd, token, formdata);
}
export function dataFindDataCol(token: string = '', formdata: any = null) {
  return customRequest(actions.dataFindDataCol, token, formdata);
}
export function matchAuthor(token: string = '', formdata: any = null) {
  return customRequest(actions.matchAuthor, token, formdata);
}
